package models

import "time"

type Permission struct {
	ID        uint   `gorm:"primaryKey"`
	Name      string `gorm:"size:100;not null"`                              // 权限名称
	Path      string `gorm:"size:255;not null"`                              // 对应前端路由或 API
	Method    string `gorm:"size:10"`                                        // GET / POST / PUT / DELETE
	Type      string `gorm:"type:enum('api','page');not null;default:'api'"` // api 或 page
	CreatedAt time.Time
	UpdatedAt time.Time
}

func GetPagePermissionsByRoleID(roleID uint) ([]Permission, error) {
	var permissions []Permission
	sql := `
		SELECT p.id, p.name, p.path
		FROM permissions p
		JOIN role_permissions rp ON rp.permission_id = p.id
		WHERE rp.role_id = ? AND p.type = 'page'
	`
	if err := DB.Raw(sql, roleID).Scan(&permissions).Error; err != nil {
		return nil, err
	}
	return permissions, nil
}
